package com.example.application1.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.application1.API.newsApi;
import com.example.application1.API.weatherApi;
import com.example.application1.Adapter.weatherAdapter;
import com.example.application1.Entity.Bean;
import com.example.application1.Entity.NewslistDTOXX;
import com.example.application1.Entity.TianQiBean;
import com.example.application1.R;
import com.example.application1.tools.DividerGridItemDecoration;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class weatherFragment extends Fragment {

    private View view;
    private List<NewslistDTOXX> weatherList;
    private RecyclerView weatherRecycle;
    private weatherAdapter Adapter;
    private EditText edit_weatherNumbers;
    private Button weatherSubmit;
    private Button btn_jump;
    private TextView sx;
    private TextView id_tv_nowtemp;
    private String city="贵阳";



    public static weatherFragment newInstance() {

        Bundle args = new Bundle();
        weatherFragment fragment = new weatherFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      view=inflater.inflate(R.layout.weather_fragment,container,false);
      return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        requestWeatherData(city);
        weatherSubmit=view.findViewById(R.id.weatherSubmit);
        weatherSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestWeatherData(edit_weatherNumbers.getText().toString());
            }
        });
        edit_weatherNumbers=view.findViewById(R.id.edit_weatherNumbers);
        sx=view.findViewById(R.id.id_tv_city);
        id_tv_nowtemp=view.findViewById(R.id.id_tv_nowtemp);
        weatherRecycle=view.findViewById(R.id.weatherRecycle);
        btn_jump=view.findViewById(R.id.btn_jump);
        btn_jump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        weatherRecycle.setLayoutManager(new LinearLayoutManager(getContext()));
        weatherRecycle.addItemDecoration(new DividerGridItemDecoration(getContext()));
        Adapter=new weatherAdapter(getContext(),weatherList);
        weatherRecycle.setAdapter(Adapter);
    }

    //网络数据请求方法
    private void requestWeatherData(String city) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://api.tianapi.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        weatherApi service = retrofit.create(weatherApi.class);
        Call<TianQiBean> call = service.getCall("1fe590b621dcb1472141d1dee8323c85",city);
        call.enqueue(new Callback<TianQiBean>() {
            @Override
            public void onResponse(Call<TianQiBean> call, Response<TianQiBean> response) {
                weatherList=response.body().getNewslist();
                sx.setText(weatherList.get(0).getArea());
                id_tv_nowtemp.setText(weatherList.get(0).getReal());
                weatherRecycle.smoothScrollToPosition(weatherList.size());
                Adapter.refresh(weatherList);
            }
            @Override
            public void onFailure(Call<TianQiBean> call, Throwable t) {
                System.out.println("连接失败");
            }
        });
    }
}
